<!DOCTYPE html>
<meta charset="utf-8">
<title>Named access on the window object - Existing property</title>
<link rel="author" title="Matthew Phillips" href="mailto:matthew@matthewphillips.info">
<link rel="help" href="https://html.spec.whatwg.org/#named-access-on-the-window-object">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<body>

<script>
"use strict";

test(() => {
  const tags = [
    "a", "area", "embed", "form", "frameset", "img",
    "A", "AREA", "EMBED", "FORM", "FRAMESET", "IMG"
  ];

  for (const tagName of tags) {
    const element = document.createElement(tagName);
    element.setAttribute("name", "positive");
    document.body.appendChild(element);
  }

  tags.forEach((tagName, index) => {
    const element = window.positive[index];
    assert_true(element && element.nodeName.toUpperCase() === tags[index].toUpperCase());
  });
}, "Only elements with specific tag names may be returned if their name attribute matches");

test(() => {
  // Not exhaustive
  const tags = [
    "div", "span", "style", "script", "h1", "strong", "input",
    "audio", "button", "select", "textarea"
  ];

  for (const tagName of tags) {
    const element = document.createElement(tagName);
    element.setAttribute("name", "negative");
    document.body.appendChild(element);
  }

  assert_false(Boolean(window.negative));
}, "Only elements with specific tag names may be returned if their name attribute matches (negative test)");

test(() => {
  // Not exhaustive
  const tags = [
    "div", "span", "style", "script", "h1", "strong", "input",
    "audio", "button", "select", "textarea"
  ];

  const img = document.createElement("img");
  img.setAttribute("name", "removed");
  document.body.appendChild(img);

  for (const tagName of tags) {
    const element = document.createElement(tagName);
    element.setAttribute("name", "removed");
    document.body.appendChild(element);
    document.body.removeChild(element);
  }

  assert_equals(window.removed, img);
}, "Removing an element for which the name attribute is not tracked, should not cause errors upon removing it");
</script>
</body>
